﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace SopCastRecorder
{
	public class SopCast
	{
		public SopCastBody SopCastBody1 { get; set; }
		public SopCastPlayer SopCastPlayer1 { get; set; }
		public SopCastSaveDialog SopCastSaveDialog1 { get; set; }

		public void Start()
		{
			var befores = Win32Api.GetHandles();
			Process.Start(OptionFile.Option1.SopCastPath);
			while (true)
			{
				var newProcess = GetNewProcess(befores);
				this.SopCastBody1 = SopCastBody.GetInstance(newProcess);

				if (this.SopCastBody1 == null)
				{
					Thread.Sleep(100);
				}
				else
				{
					this.SopCastBody1.SopCast1 = this;
					break;
				}
			}
		}

		public static IntPtr[] GetNewProcess(IntPtr[] befores)
		{
			var afters = Win32Api.GetHandles();
			var newProcess = new List<IntPtr>();
			foreach (var after in afters)
			{
				if (befores.Contains(after))
				{
					continue;
				}
				newProcess.Add(after);
			}
			return newProcess.ToArray();
		}
	}
}
